package com.backstage.controller;

import com.backstage.pojo.ContactsRelationUser;
import com.backstage.pojo.ContactsUser;
import com.backstage.pojo.ObjectContactsUser;
import com.backstage.service.*;
import com.backstage.utils.JsonResult;
import com.backstage.utils.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping(value = "/joinadmin")
public class JoinAdminController {

    private static final Logger logger = LoggerFactory.getLogger(JoinWeixinAppController.class);


    @Autowired
    private ContactsUserService contactsUserService;

    /**
     * 管理后台 首页 列表
     * @param page
     * @param pageSize
     * @param tag
     * @param model
     * @return
     */
    @RequestMapping(value = {"", "/", "index"}, method = RequestMethod.GET)
    public String joinadminindex(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "10") int pageSize,
            @RequestParam(required = false, defaultValue = " ") String tag,
            Model model) {
        PageBean<ContactsUser> pageBean = contactsUserService.selectUserList(page, pageSize, tag);
        model.addAttribute("list", pageBean.getList());
        model.addAttribute("pager", pageBean.getPageBeanObj());
        return "joinadmin/index";
    }

    @RequestMapping(value = {"/test"}, method = RequestMethod.GET)
    public String joinadmintest() {
        return "joinadmin/test";
    }

    /**
     * 某个用户详情
     * @param userId 用户id
     * @param tagName 标签名称
     * @param model
     * @return
     */

    @RequestMapping(value = "{userId}/userDetails", method = RequestMethod.GET)
    public String userDetailsList(@PathVariable("userId") Integer userId, @RequestParam(required = false, defaultValue = "") String tagName, Model model){
        int totalScore = 0;
        List<ContactsRelationUser> userDetailsList = contactsUserService.selectUserDetailsList(userId, "");
        totalScore += userDetailsList.size() * 100;
        for(ContactsRelationUser item : userDetailsList){
            Integer id = item.getId();
            List<ContactsRelationUser> newContactsUser = contactsUserService.selectUserDetailsList(id, tagName);
            totalScore += newContactsUser.size() * 10;
            item.setContacts_relation_user(newContactsUser);
        }
        model.addAttribute("userDetailsList", userDetailsList);
        model.addAttribute("totalScore", totalScore);
        model.addAttribute("userId", userId);
        return "joinadmin/userDetails";
    }

    /**
     * .查询用户标签列表
     *
     * @param userid  用户id
     * @return
     */
    @RequestMapping(value = "{userid}/tablist", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult userTabList(@PathVariable("userid") Integer userid
    ) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setSuccess(false);
        try {
            List<ObjectContactsUser> tagList = contactsUserService.selectUserTagList(userid);
            jsonResult.setData(tagList);
            return jsonResult;

        } catch (Exception e) {
            logger.error("获取用户列表错误-" + e.getMessage(), e);
            jsonResult.setCode("systemException");
            jsonResult.setMessage("获取信息失败！");
            return jsonResult;
        }
    }

    /**
     * 为用户添加 Tab 标签
     * @param userid
     * @param tagname
     * @return
     */
    @RequestMapping(value = "{userid}/setTab", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult setTab(@PathVariable("userid") Integer userid, @RequestParam(required = true) String tagname) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setSuccess(false);

        Integer tagRelationTotal = contactsUserService.selectTagRelationTotal(userid);
        if(tagRelationTotal >= 20){
            jsonResult.setCode("201");
            jsonResult.setMessage("标签不能超过20个");
            return jsonResult;

        }else{
            try {
                List<ObjectContactsUser> tagList = contactsUserService.insertContactsTag(userid, tagname);
                jsonResult.setCode("200");
                jsonResult.setData(tagList);
                return jsonResult;

            } catch (Exception e) {
                logger.error("添加标签失败-" + e.getMessage(), e);
                jsonResult.setCode("systemException");
                jsonResult.setMessage("添加标签失败！");
                return jsonResult;
            }
        }
    }

    /**
     * 删除标签
     * @param userId 用户Id
     * @param tagId 标签ID
     * @return
     */
    @RequestMapping(value = "{userId}/deleteTag", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult deleteTag(@PathVariable("userId") int userId, @RequestParam(required = true) int tagId){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setSuccess(false);
        try {
            List<ObjectContactsUser> tagList = contactsUserService.deleteContactsTag(userId, tagId);
            jsonResult.setCode("200");
            jsonResult.setData(tagList);
            return jsonResult;

        } catch (Exception e) {
            logger.error("删除标签失败-" + e.getMessage(), e);
            jsonResult.setCode("systemException");
            jsonResult.setMessage("删除标签失败！");
            return jsonResult;
        }
    }


}
